﻿<!--
 Copyright 2013 Shawn Henry, Rob Smith, and Michael Friedman

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<view:AViewPage
    x:Class="OneBusAway.WP7.View.StopsMapPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:m="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="clr-namespace:OneBusAway.WP7.ViewModel;assembly=OneBusAway.WP7.ViewModel" 
    xmlns:view="clr-namespace:OneBusAway.WP7.View"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    xmlns:tilt="clr-namespace:ControlTiltEffect"
    tilt:TiltEffect.IsTiltEnabled="True"
    shell:SystemTray.IsVisible="True"
    xmlns:localControls="clr-namespace:Microsoft.Phone.Controls"
    >

    <view:AViewPage.Resources>
        <data:StopsMapVM x:Key="ViewModel" />
        <view:VisibilityConverter x:Key="VisibilityConverter"></view:VisibilityConverter>
        <view:MaxZoomConverter x:Key="MaxZoomConverter"></view:MaxZoomConverter>
        <view:ColorAlphaConverter x:Key="ColorAlphaConverter"/>

        <Style x:Key="BusStopLargePushpinStyle" TargetType="m:Pushpin">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Button 
                            Width="65" Height="65"
                            HorizontalContentAlignment="Stretch" 
                            VerticalContentAlignment="Stretch" 
                            BorderThickness="0" 
                            Padding="0" 
                            Click="BusStopPushpin_Click"
                            Tag="{TemplateBinding Tag}"
                            >
                            <Image Source="/Bus_Icon.png" Margin="-3"/>
                        </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        
    </view:AViewPage.Resources>

    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="4"/>
            <RowDefinition Height="27"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <!--ScaleVisibility="Collapsed"-->
        <m:Map Grid.Row="1" Grid.RowSpan="2" Name="DetailsMap" CopyrightVisibility="Collapsed" LogoVisibility="Collapsed"  CredentialsProvider="{StaticResource MapCredentials}"
                Center="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}"  ZoomLevel="17" >
            <m:MapLayer x:Name="BusStopsLayer" Canvas.ZIndex="2" Visibility="{Binding Path=ZoomLevel, ElementName=DetailsMap, Converter={StaticResource MaxZoomConverter}, ConverterParameter=false}">
                <m:MapItemsControl x:Name="StopsMapItemsControl" ItemsSource="{Binding Path=StopsForLocation, Source={StaticResource ViewModel}}">
                    <m:MapItemsControl.ItemTemplate>
                        <DataTemplate>
                            <m:Pushpin Location="{Binding location}" PositionOrigin="Center" Style="{StaticResource BusStopLargePushpinStyle}" Tag="{Binding id}" />
                        </DataTemplate>
                    </m:MapItemsControl.ItemTemplate>
                </m:MapItemsControl>
            </m:MapLayer>
            <m:MapLayer x:Name="PopupLayer" Canvas.ZIndex="10" Visibility="{Binding Path=Visibility, ElementName=BusStopsLayer}">
                <m:Pushpin x:Name="StopInfoBox" Visibility="Collapsed">
                    <Button 
                        x:Name="PopupBtn"
                        HorizontalContentAlignment="Stretch"
                        VerticalContentAlignment="Stretch"
                        BorderThickness="0"
                        Padding="0"
                        Margin="-5,-5,-5,-5"
                        Background="{StaticResource OBABackgroundBrush}"
                        Click="PopupBtn_Click"
                            >
                        <Grid Margin="4" Background="{StaticResource OBAPrimaryBrush}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <TextBlock Name="StopName" Grid.Row="0" Foreground="{StaticResource OBAForegroundBrush}" Margin="2,2,4,2" />
                            <TextBlock Name="StopRoutes" Grid.Row="1" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Margin="2"/>
                            <TextBlock Name="StopDirection" Grid.Row="2" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Margin="2,0,2,2"/>
                        </Grid>
                    </Button>
                </m:Pushpin>
            </m:MapLayer>
            <m:MapLayer x:Name="MyLocationLayer" Canvas.ZIndex="8">
                <m:MapLayer.Children>
                    <m:Pushpin Location="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}" PositionOrigin="Center" Style="{StaticResource MyLocationPushpinStyle}" />
                </m:MapLayer.Children>
            </m:MapLayer>
        </m:Map>
        
        <Border 
            x:Name="TooManyStops" 
            Background="{Binding Source={StaticResource OBADarkBrush}, Converter={StaticResource ColorAlphaConverter}, ConverterParameter={StaticResource OBABackgroundBrush}}" 
            Grid.Row="1"
            Visibility="{Binding ElementName='DetailsMap', Path='ZoomLevel', Converter={StaticResource MaxZoomConverter}, ConverterParameter=true}"
            >
            <TextBlock Grid.Row="1" Style="{StaticResource PhoneTextSmallStyle}" Foreground="{StaticResource OBAForegroundBrush}"
                       Text="Too many stops. Zoom in to see nearby stops." />
        </Border>

        <localControls:PerformanceProgressBar 
            VerticalAlignment="Top" 
            IsLoading="{Binding Path=operationTracker.Loading, Source={StaticResource ViewModel}}"
            Style="{StaticResource PerformanceProgressBarStyle}"
            Foreground="{StaticResource OBAPrimaryBrush}"     
            Canvas.ZIndex="100"
            />
    </Grid>

</view:AViewPage>
